package com.heima.wemedia.controller.v1;

import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.impl.WmNewsAutoScanServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {
    @Autowired
    private WmNewsService wmNewsService;
    @PostMapping("/list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto){
        return wmNewsService.findAll(dto);
    }

    /**
     * 文章发布
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto){
        return wmNewsService.submitNews(dto);
    }

    /**
     * 发布文章上下架
     */
    @PostMapping("/down_or_up")
    public ResponseResult downorup(@RequestBody WmNewsDto wmNewsDto ){
       return  wmNewsService.downorup(wmNewsDto);
    }

    /**
     * 自媒体文章分页查询
     * @param dto
     * @return
     */
    @PostMapping("list_vo")
    public PageResponseResult ListVO(@RequestBody NewsAuthDto dto){
      return wmNewsService.ListVO(dto);
    }

    /**
     * 自媒体文章详情
     * @param id
     * @return
     */
    @GetMapping("one_vo/{id}")
    public ResponseResult oneVo(@PathVariable Long id){
        WmNews wmNews = wmNewsService.getById(id);
        return ResponseResult.okResult(wmNews);
    }

    /**
     * 人工审核失败
     * @param dto
     * @return
     */
    @PostMapping("/auth_fail")
    public ResponseResult  Authfail(@RequestBody NewsAuthDto dto){
        WmNews wmNews = new WmNews();
        wmNews.setReason(dto.getMsg());
        wmNews.setId(dto.getId());
        wmNews.setStatus((short)2);
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
@Autowired
private WmNewsAutoScanServiceImpl wmNewsAutoScanServiceimpl;
    /**
     * 人工审核成功
     * @param dto
     * @return
     */
    @PostMapping("/auth_pass")
    @Transactional
    public ResponseResult  Authpass(@RequestBody NewsAuthDto dto){
        WmNews wmNews = new WmNews();
//        wmNews.setReason(dto.getMsg());
        wmNews.setId(dto.getId());
        wmNews.setStatus((short)4);
        wmNewsService.updateById(wmNews);
        WmNews wmNews1 = wmNewsService.getById(dto.getId());
        wmNewsAutoScanServiceimpl.saveAppArticle(wmNews1);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}